package cn.adelyn.blog.blogmanager.dao;

import cn.adelyn.blog.blogmanager.dao.mapper.BlogPicMapper;
import cn.adelyn.blog.blogmanager.pojo.bo.InsertBlogDecryptBO;
import cn.adelyn.blog.blogmanager.pojo.model.BlogPicModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class BlogPicDAOService {

    @Autowired
    BlogPicMapper blogPicMapper;

    public void insertBlogPics(InsertBlogDecryptBO insertBlogDecryptBO) {
        List<BlogPicModel> blogPicModelList = genBlogPicModelList(insertBlogDecryptBO);
        blogPicMapper.insertBatch(blogPicModelList);
    }

    public List<Long> selectBlogPics(Long blogId) {
        QueryWrapper<BlogPicModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BlogPicModel::getBlogId, blogId);

        return blogPicMapper.listBlogPics(queryWrapper);
    }

    public void deleteBlogPics(Long blogId) {
        QueryWrapper<BlogPicModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BlogPicModel::getBlogId, blogId);

        blogPicMapper.delete(queryWrapper);
    }

    private List<BlogPicModel> genBlogPicModelList(InsertBlogDecryptBO insertBlogDecryptBO) {
        List<BlogPicModel> blogPicModelList = new ArrayList<>();
        insertBlogDecryptBO.getPicIdList().stream().forEach(picId -> {
            BlogPicModel blogPicModel = new BlogPicModel();
            blogPicModel.setBlogId(insertBlogDecryptBO.getBlogId());
            blogPicModel.setPicId(picId);
            blogPicModelList.add(blogPicModel);
        });
        return blogPicModelList;
    }
}
